//
//  BQGoodsAddressController.m
//  我的收货地址
//
//  Created by mac on 16/11/19.
//  Copyright © 2016年 解延峰. All rights reserved.
//

#import "BQGoodsAddressController.h"
#import "UIColor+CZAddition.h"
#import "BQGoodsAddressCell.h"
#import "AFNetworking.h"
#import "BQGoodsAddressModel.h"
#import "YYModel.h"
#import "Masonry.h"
#import "BQAddRessTableVC.h"

static NSString * cellID = @"cellID";
@interface BQGoodsAddressController ()<UITableViewDelegate,UITableViewDataSource,AddRessTableDelegate>

@property(nonatomic,strong)NSMutableArray<BQGoodsAddressModel*> * arrayList;

@property(nonatomic,weak)UITableView *tableView;

@property (strong,nonatomic)UIButton *addBtn;

@end

@implementation BQGoodsAddressController

-(NSMutableArray<BQGoodsAddressModel *> *)arrayList
{
    if (_arrayList == nil)
    {
        _arrayList = [NSMutableArray array];
    }
    return _arrayList;
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // MARK:隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    
    //MARK:导航返回按钮 替换扫一扫
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonOF:)];
    self.navigationItem.leftBarButtonItem = barButtonItem;

    [self setupUI];
    [self getLoadData];
    //MARK: 接收删除的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(del:) name:@"delete" object:nil];
    
    
}
//MARK: 返回上一个界面
- (void)buttonOF:(UIBarButtonItem *)batBtn{
    
    [self.navigationController popViewControllerAnimated:YES]; }
//MARK:删除通知方法

- (void)del:(NSNotification*)no
{
    
    NSDictionary * dic = no.userInfo;
    NSIndexPath * index = dic[@"delete"];
    [self.arrayList removeObjectAtIndex:index.row];
    [self.tableView reloadData];
    [self.navigationController popViewControllerAnimated:YES];
}
// MARK:销毁
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"delete" object:nil];
}

//MARK:代理方法 接收传多来的数据
-(void)addRessTableVC:(BQAddRessTableVC *)name addModel:(BQGoodsAddressModel *)model
{
    if (![self.arrayList containsObject:model])
    {
        [self.arrayList insertObject:model atIndex:0];
    }

    
    [self.tableView reloadData];
   
}


//UI布局
-(void)setupUI{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"我的收货地址";
    
    //创建tableview
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 10, self.view.bounds.size.width, self.view.bounds.size.height-60) style:UITableViewStylePlain];
    //取消分割线
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    
    
    [tableView registerClass:[BQGoodsAddressCell class] forCellReuseIdentifier:cellID];
    tableView.backgroundColor = [UIColor cz_colorWithHex:0xefefef];
    
    //创建button
    UIButton * addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //颜色
    addBtn.backgroundColor = [UIColor cz_colorWithHex:0xfdd431];
    //圆角
    addBtn.layer.cornerRadius  = 8.0;
    //文本
    [addBtn setTitle:@"+新增地址" forState:UIControlStateNormal];
    addBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [addBtn setTitleColor:[UIColor cz_colorWithHex:0x323232] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(pushToAdd:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:addBtn];
    
    //关联
    self.tableView =tableView;
    self.addBtn = addBtn;
    
    
    //布局
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.view).offset(-10);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(240);
        make.height.mas_equalTo(30);
    }];
    
}
//加载网络数据
-(void)getLoadData{
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",nil];
    
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    NSDictionary *dict = @{@"call":@"12"};
    [manager POST:@"http://iosapi.itcast.cn/loveBeen/MyAdress.json.php" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    
        
        NSArray<BQGoodsAddressModel *> *array = [NSArray yy_modelArrayWithClass:[BQGoodsAddressModel class] json:responseObject[@"data"]];
        
#warning 可变转换不可变
            self.arrayList = [NSMutableArray arrayWithArray:array];
            [self.tableView reloadData];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error%@",error);
    }];
}

//跳转到添加新地址
-(void)pushToAdd:(UIButton *)sender{
    
    BQAddRessTableVC * addVc = [[BQAddRessTableVC alloc]init];
    addVc.delegate = self;
    
    [self.navigationController pushViewController:addVc animated:YES];
}


//数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    
    return self.arrayList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BQGoodsAddressCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    BQGoodsAddressModel * model = self.arrayList[indexPath.row];
    
    cell.model = model;
    

    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}


//MARK:传出数据
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BQAddRessTableVC * addVc = [[BQAddRessTableVC alloc]init];
    addVc.Addmodel = self.arrayList[indexPath.row];
    addVc.indexpOne = indexPath;
    
    
    
    [self.navigationController pushViewController:addVc animated:YES];
    
}

//视图将要被移除   显示tabbar
- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
    
}
//视图将要显示  隐藏tabbar
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
    
}

@end
